<template>
	<div class="app-container">
		<el-tabs v-model="activeName" type="card" @tab-click="handleClick">
			<el-tab-pane label="余额支付配置" name="first">
				<div class="titleRigId mt-md mt-mr row" v-if="payConfigList[0]">
					<div class="radioLable">是否开启余额支付：</div>
					<el-radio-group v-model="payConfigList[0].enable">
						<el-radio v-for="dict in dict.type.is_enable" :key="dict.value"
							:label="parseInt(dict.value)">{{dict.label}}</el-radio>
					</el-radio-group>
				</div>
				<el-form :rules="rules" label-width="220px">
					<el-form-item prop="value">
						<span slot="label">
							<span class="item__label">余额图标：</span>
						</span>
						<image-upload v-model="payConfigList[0].icon" :limit="1" />
					</el-form-item>
				</el-form>
				<el-button type="primary" size="medium" @click="handleQuery">提 交</el-button>
			</el-tab-pane>


			<el-tab-pane label="支付宝配置" name="second">
				<div class="titleRigId mt-md mt-mr row" v-if="payConfigList[2]">
					<div class="radioLable">是否开启支付宝支付：</div>
					<el-radio-group v-model="payConfigList[2].enable">
						<el-radio v-for="dict in dict.type.is_enable" :key="dict.value"
							:label="parseInt(dict.value)">{{dict.label}}</el-radio>
					</el-radio-group>
				</div>
				<el-form :rules="rules" label-width="220px">
					<el-form-item prop="value">
						<span slot="label">
							<span class="item__label">支付宝图标：</span>
						</span>
						<image-upload v-model="payConfigList[2].icon" :limit="1" />
					</el-form-item>
				</el-form>

				<div class="titleRigId mt-md mt-mr row">
					<div class="radioLable">支付宝模式：</div>
					<el-radio-group v-model="radioEadn">
						<el-radio v-for="dict in radioTxt" :key="dict.value"
							:label="parseInt(dict.value)">{{dict.label}}</el-radio>
					</el-radio-group>

				</div>

				<el-form :rules="rules" label-width="220px" v-if="radioEadn">
					<el-form-item prop="value" v-for="item in zfbpayConfigList.slice(0,2)" :key="item.id">
						<span slot="label">
							<span class="item__label">{{item.name}}：</span>
						</span>
						<el-input v-model="item.value" placeholder="请输入value" class="elInput" />
					</el-form-item>
				</el-form>

				<el-form :rules="rules" label-width="220px" v-if="radioEadn==1">
					<el-form-item prop="value" v-for="item in zfbpayConfigList.slice(2,3)" :key="item.id">
						<span slot="label">
							<span class="item__label">{{item.name}}：</span>
						</span>
						<el-input v-model="item.value" placeholder="请输入value" class="elInput" />
					</el-form-item>
				</el-form>

				<el-form :rules="rules" label-width="220px" v-if="radioEadn==2">
					<el-form-item prop="value" v-for="item in zfbpayConfigList.slice(3,6)" :key="item.id">
						<span slot="label">
							<span class="item__label">{{item.name}}：</span>
						</span>
						<el-input v-model="item.value" placeholder="请输入value" class="elInput" />
						<FileUpload :fileType="['crt' ,'csr']" :which="item.id" @upLoadSucc="upLoadSucc" :limit="1" />
					</el-form-item>
				</el-form>
				<el-button type="primary" size="medium" @click="handleQuery">提 交</el-button>
			</el-tab-pane>


			<el-tab-pane label="微信配置" name="third">
				<div class="titleRigId mt-md mt-mr row" v-if="payConfigList[1]">
					<div class="radioLable">是否开启微信支付：</div>
					<el-radio-group v-model="payConfigList[1].enable">
						<el-radio v-for="dict in dict.type.is_enable" :key="dict.value"
							:label="parseInt(dict.value)">{{dict.label}}</el-radio>
					</el-radio-group>
				</div>
				<el-form :rules="rules" label-width="220px">
					<el-form-item prop="value">
						<span slot="label">
							<span class="item__label">微信图标：</span>
						</span>
						<image-upload v-model="payConfigList[1].icon" :limit="1" />
					</el-form-item>
				</el-form>

				<el-form :rules="rules" label-width="220px">
					<el-form-item prop="value" v-for="item in wxpayConfigList" :key="item.id">
						<span slot="label">
							<span class="item__label">{{item.name}}：</span>
						</span>
						<el-input v-model="item.value" placeholder="请输入value" class="elInput" />
						<FileUpload v-if="item.id==4" :fileType="['pem']" @upLoadSucc="upLoadSucc" :limit="1" />
					</el-form-item>
				</el-form>

				<el-button type="primary" size="medium" @click="handleQuery">提 交</el-button>
			</el-tab-pane>
		</el-tabs>
	</div>
</template>

<script>
	import {
		number
	} from "echarts";
	import {
		listPayConfig,
		getPayConfig,
		delPayConfig,
		addPayConfig,
		updatePayConfig,
		updatePayConfigVo
	} from "@/api/payConfig/payConfig";
	import {
		listWxpayConfig
	} from "@/api/wxpayConfig/wxpayConfig";
	import {
		listZfbpayConfig
	} from "@/api/zfbpayConfig/zfbpayConfig";
	import {
		FormItem
	} from "element-ui";
	import FileUpload from "@/components/FileUpload"
	export default {
		name: "PayConfig",
		dicts: ['is_enable'],
		components: {
			FileUpload
		},
		data() {
			return {
				radioEadn: null,
				activeName: 'first',
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 支付方式配置表格数据
				payConfigList: [],
				wxpayConfigList: [],
				zfbpayConfigList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					enable: null,
				},
				radioTxt: [{
						label: '秘钥模式',
						value: '1'
					},
					{
						label: '证书模式',
						value: '2'
					}
				],
				// 表单参数
				form: {},
				zfbForm: {},
				// 表单校验
				rules: {}
			};
		},
		created() {
			this.getList();
		},
		methods: {
			/** 查询支付方式配置列表 */
			getList() {
				this.loading = true;
				listPayConfig(this.queryParams).then(response => {
					this.payConfigList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			/** 查询微信支付配置列表 */
			wxpayList() {
				this.loading = true;
				listWxpayConfig(this.queryParams).then(response => {
					this.wxpayConfigList = response.rows;
					this.loading = false;
				});
			},
			/** 查询支付宝支付配置列表 */
			zfbpayList() {
				this.loading = true;
				listZfbpayConfig(this.queryParams).then(response => {
					this.zfbpayConfigList = response.rows;
					this.radioEadn = Number(this.zfbpayConfigList[6].value)
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					id: null,
					icon: null,
					name: null,
					code: null,
					enable: null,
					sort: null,
					isDelete: null,
					createTime: null,
					updateTime: null
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				let data = []
				if (this.activeName == 'first') data.push(this.payConfigList[0])
				if (this.activeName == 'second') {
					this.zfbpayConfigList[6].value = this.radioEadn
					data = [...this.zfbpayConfigList, this.payConfigList[2]]
				}
				if (this.activeName == 'third') {
					data = [...this.wxpayConfigList, this.payConfigList[1]]
				}

				updatePayConfigVo(data).then(response => {
					this.$modal.msgSuccess("提交成功");
					this.getList();
					if (this.activeName == 'second') this.zfbpayList()
					if (this.activeName == 'third') this.wxpayList()
				});
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.id)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.open = true;
				this.title = "添加支付方式配置";
			},
			upLoadSucc(data, item) {
				if (this.activeName == 'second') {
					if (data.which == 5) this.zfbpayConfigList[3].value = data.list[0].url
					if (data.which == 6) this.zfbpayConfigList[4].value = data.list[0].url
					if (data.which == 7) this.zfbpayConfigList[5].value = data.list[0].url
				}
				if (this.activeName == 'third') {
					if (data.which == 0) this.wxpayConfigList[3].value = data.list[0].url
				}
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				const id = row.id || this.ids
				getPayConfig(id).then(response => {
					this.form = response.data;
					this.open = true;
					this.title = "修改支付方式配置";
				});
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						if (this.form.id != null) {
							updatePayConfig(this.form).then(response => {
								this.$modal.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							});
						} else {
							addPayConfig(this.form).then(response => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const ids = row.id || this.ids;
				this.$modal.confirm('是否确认删除支付方式配置编号为"' + ids + '"的数据项？').then(function() {
					return delPayConfig(ids);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			// 标签点击
			handleClick(tab, event) {
				this.activeName = tab.name
				if (this.activeName == 'second') this.zfbpayList()
				if (this.activeName == 'third') this.wxpayList()
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('payConfig/payConfig/export', {
					...this.queryParams
				}, `payConfig_${new Date().getTime()}.xlsx`)
			}
		}
	};
</script>

<style lang="less">
	.mt-md {
		margin: 5px 0;
	}

	.mt-mr {
		margin-left: 50px;
	}

	.titleRigId {
		font-size: 14px;
		margin-bottom: 20px;
	}

	.radioLable {
		width: 160px;
		text-align: right;
		color: #666;
		margin-right: 10px;
	}

	.item__label {
		width: 200px;
		text-align: right;
		color: #666;
		font-size: 14px;
		font-weight: 500;
	}
</style>